<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">
      <el-row>

        <el-form-item label="关键词" prop="commentContent">
          <el-input v-model="queryParams.commentContent" placeholder="请输入关键词" clearable size="small"
            style="width: 232px" class="input-rule" />
        </el-form-item>

        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable size="small" style="width: 232px"
            class="input-rule" />
        </el-form-item>

        <el-form-item label="商品分类" prop="categoryUid">
          <el-cascader :props="{ checkStrictly: true }" v-model="queryParams.categoryUid" :options="categoryOptions"
            clearable filterable style="width: 232px" class="input-rule">
          </el-cascader>
        </el-form-item>

      </el-row>
      <el-row>

        <el-form-item label="评论时间" prop="applyAuthTimeArray">
          <el-date-picker v-model="queryParams.applyAuthTimeArray" type="daterange" align="right" style="width: 232px"
            value-format="yyyy-MM-dd" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>

    </el-form>

    <el-row :gutter="10" class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['product:buyersshow:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['product:buyersshow:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:buyersshow:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['product:buyersshow:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table v-loading="loading" :data="buyersshowList" border style="font-size:12px"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="买家秀内容" align="left" min-width="300">
          <template slot-scope="scope">
            <div align="left" class="line_div">{{ scope.row.commentContent }}</div>
            <div align="left" class="show_commment_img"
              v-if="scope.row.carouselImgs != null && scope.row.carouselImgs != '' " slot="reference">
              <!-- <span>上传图片：</span> -->
              <el-image style="display:block;margin: 5px 5px 0 0" v-for="(imgs,index) in scope.row.carouselImgs"
                :key="index" :src="imgs" class="image-rows" :preview-src-list="scope.row.carouselImgs">
              </el-image>
            </div>
            <div align="left" class="line_div" style="color: #999">{{ scope.row.commentTime }}</div>
          </template>
        </el-table-column>

        <el-table-column label="商品信息" align="left" min-width="200">
          <template slot-scope="scope">
            <div class="show_pro_info">
              <div align="left">
                <el-image v-if="scope.row.productImgUrl != null && scope.row.productImgUrl != '' " slot="reference"
                  :src="scope.row.productImgUrl" class="image-rows" :preview-src-list="[scope.row.productImgUrl]">
                </el-image>
              </div>
              <div style="margin-left: 5px; flex: 1;overflow: hidden">
                <div class="show_pro_title" size="mini" type="text" @click="handledetail(scope.row)">
                  <el-tooltip class="item" effect="light" :content="scope.row.productName" placement="top">
                    <span class="show_pro_title_txt">{{ scope.row.productName }}</span>
                  </el-tooltip>

                </div>
                <span>{{ scope.row.valuesDesc }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

          <el-table-column label="买家秀信息" align="left" min-width="100">
            <template slot-scope="scope">
              <div class="show_pro_info">
                <div align="left">
                  <el-image v-if="scope.row.buyImg != null && scope.row.buyImg != '' " slot="reference"
                    :src="scope.row.buyImg" class="image-rows" :preview-src-list="[scope.row.buyImg]">
                  </el-image>
                </div>
                <div style="margin-left: 5px; flex: 1;overflow: hidden">
                  <div class="show_pro_title" size="mini" type="text">
                    <el-tooltip class="item" effect="light" :content="scope.row.buyName" placement="top">
                      <span class="show_pro_title_txt">{{ scope.row.buyName }}</span>
                    </el-tooltip>
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>

        <el-table-column width="150" label="操作" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">

            <el-button v-if="scope.row.commentVideo" style="display: block;margin: 0" size="mini" type="text"
              icon="el-icon-video-play" @click="playVideo(scope.row.commentVideo)">查看评论视频
            </el-button>

            <el-button v-if="scope.row.isShow ==  '0'" size="mini" type="text" icon="el-icon-check"
              style="display: block;margin: 0" @click="pin(scope.row,'1')">显示
            </el-button>
            <el-button v-if="scope.row.isShow ==  '1' " size="mini" type="text" icon="el-icon-close"
              style="display: block;margin: 0" @click="pin(scope.row,'0')">屏蔽
            </el-button>
            <el-button style="display: block;margin: 0" size="mini" type="text" icon="el-icon-edit"
              @click="handleUpdate(scope.row)">修改</el-button>
          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>


    <!-- 添加或修改商品买家秀信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @keyup.enter.native="submitForm"
      @close="hahas">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商品信息" prop="productUid">
          <el-button @click="selPro">选择商品</el-button>
          <!-- 已选商品列表组件-->
          <selectedPro v-if="proTableOpen" @tabRespon="tabFun" :tabList="tabList" :tabUids="tabUids" :isShow="false"
            :destroy-on-close="true">
          </selectedPro>

        </el-form-item>

        <el-form-item label="是否显示" prop="isShow">
          <el-radio-group v-model="form.isShow">
            <el-radio label="1">显示</el-radio>
            <el-radio label="0">屏蔽</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="买家秀名称" prop="buyName">
          <el-input v-model="form.buyName" placeholder="买家秀名称" clearable
            />
        </el-form-item>


        <el-form-item label="买家秀头像" prop="buyImg">
          <imageArraySize :imageStr.sync="form.buyImg" :key="form.imagesUrl" ref="childss"
            :msg="'图片要求：宽高等于800px，大小1M内。'" :limit="1"></imageArraySize>
        </el-form-item>

        <el-form-item label="图片地址" prop="imagesUrl">
          <imageArraySize :imageStr.sync="form.imagesUrl" :key="form.imagesUrl" ref="childs"
            :msg="'图片要求：宽高等于800px，大小1M内。'" :limit="10"></imageArraySize>
        </el-form-item>

        <el-form-item label="评论视频" prop="commentVideo" style="height: 130px;">
          <div class="place-font">视频要求：大小8M内。</div>
          <videoUpload :videoStr.sync="form.commentVideo"></videoUpload>
        </el-form-item>

        <el-form-item label="内容">
          <el-input type="textarea" :rows="2" v-model="form.commentContent" placeholder="请输入商品描述" clearable
            maxlength="200" show-word-limit />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title :visible.sync="dialogPlay" @close="closeDialog">
      <video :src="videoUrl" controls autoplay class="video" style="height:700px;width:100%"></video>
    </el-dialog>

    <!-- 选择商品对话框-->
    <el-dialog title="选择商品" :visible.sync="selOpen" width="1000px" append-to-body :destroy-on-close="true">
      <productRadio v-if="selOpen" @idsId="idsId" :uids="selUids"></productRadio>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listBuyersshow,
    getBuyersshow,
    delBuyersshow,
    addBuyersshow,
    updateBuyersshow
  } from "@/api/product/buyersshow";
  import Editor from '@/components/Editor';
  import {
    selectOptions
  } from "@/api/product/category";
  import imageArraySize from "@/components/ImageUpload/imageArraySize";
  import videoUpload from "@/components/ImageUpload/videoUpload";
  import selectedPro from '@/components/Activity/selectedPro';
  import productRadio from '@/components/Product/productRadio';

  export default {
    name: "Buyersshow",
    components: {
      Editor,
      imageArraySize,
      videoUpload,
      selectedPro,
      productRadio
    },
    data() {
      return {
        proTableOpen: false,
        // 商品分类
        categoryOptions: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近六个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 180)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        // 商品买家秀信息表格数据
        buyersshowList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态字典
        dataStatusOptions: [],
        // 查询参数
        queryParams: {
          applyAuthTimeArray: [],
          pageNum: 1,
          pageSize: 10,
          imagesUrl: null,
          commentContent: null,
          commentVideo: null,
          commentTime: null,
          productUid: null,
          productName: null,
          productImgUrl: null,
          dataStatus: null,
          isShow: null,
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null
        },
        columns: [{
            label: '更新时间',
            dataIndex: 'buyersshowUid',
          },
          {
            label: '图片地址，多种以,逗号分开',
            dataIndex: 'imagesUrl',
          },
          {
            label: '内容',
            dataIndex: 'commentContent',
          },
          {
            label: '评论视频',
            dataIndex: 'commentVideo',
          },
          {
            label: '评论时间',
            dataIndex: 'commentTime',
          },
          {
            label: '商品UID',
            dataIndex: 'productUid',
          },
          {
            label: '商品名称',
            dataIndex: 'productName',
          },
          {
            label: '商品图片',
            dataIndex: 'productImgUrl',
          },
          {
            label: '状态（1有效-1删除',
            dataIndex: 'dataStatus',
          },
          {
            label: '是否显示（1显示,0隐藏）',
            dataIndex: 'isShow',
          },
          {
            label: '创建人ID',
            dataIndex: 'createUid',
          },
          {
            label: '创建人名称',
            dataIndex: 'createName',
          },
          {
            label: '创建时间',
            dataIndex: 'createTime',
          },
          {
            label: '更新人ID',
            dataIndex: 'updateUid',
          },
          {
            label: '更新人名称',
            dataIndex: 'updateName',
          },
          {
            label: '更新时间',
            dataIndex: 'updateTime',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          buyName: [{
            required: true,
            message: "买家秀名称不能为空",
            trigger: "blur"
          }],
        },
        dialogPlay: false,
        videoUrl: null,
        selOpen: false,
        selUids: "",
        tabUids: [],
        tabList: [],
      };
    },
    created() {
      // 默认时间
      let date1 = new Date();
      let date2 = new Date(date1);
      date2.setDate(date1.getDate() - 90);
      let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
      let time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
      this.queryParams.applyAuthTimeArray.push(time2);
      this.queryParams.applyAuthTimeArray.push(time1);
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.dataStatusOptions = response.data;
      });

      selectOptions().then(response => {
        this.categoryOptions = response.data;
      });
    },
    methods: {
      pin(row, obj) {
        let data = {
          buyersshowUid: row.buyersshowUid,
          isShow: obj,
          product: row.productUid
        };
        updateBuyersshow(data).then(response => {
          this.$message.info("操作成功！")
          this.getList();
        });
      },
      hahas() {
        this.tabUids = [];
        this.tabList = [];
      },
      tabFun(data, v) {
        this.selUids = "";
        if (v)
          this.selUids = v[0]; //同步已选商品ID
        this.tabUids = v;
        if (data) {
          if (data.length == 0) {
            this.form.productUid = null;
            this.form.productName = null;
            this.form.productImgUrl = null;
            this.proTableOpen = false;
          } else {
            this.form.productUid = data.productUid;
            this.form.productName = data.productName;
            this.form.productImgUrl = data.productImg;
          }
        }
      },
      idsId(ids) {
        this.$forceUpdate(); //强制刷新
        if (ids && ids.productUid) {
          this.tabUids = [];
          this.tabList = [];
          this.proTableOpen = false;

          this.selUids = ids.productUid;

          this.tabUids.push(ids.productUid);
          this.tabList.push(ids);

          this.form.productUid = ids.productUid;
          this.form.productName = ids.productName;
          this.form.productImgUrl = ids.productImg;

          this.$nextTick().then(() => {
            this.proTableOpen = true;
          })

        }
        this.selOpen = false;
      },
      selPro() {
        this.selOpen = true;
      },
      handledetail(row) {
        const productUid = row.productUid
        this.$router.push("/product/detail/" + productUid);
      },
      closeDialog() {
        this.dialogPlay = false;
        this.videoUrl = ''
      },
      playVideo(v) {
        this.dialogPlay = true;
        this.videoUrl = v;
      },
      /** 查询商品买家秀信息列表 */
      getList() {
        this.loading = true;
        listBuyersshow(this.queryParams).then(response => {
          this.buyersshowList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          buyersshowUid: null,
          imagesUrl: null,
          commentContent: null,
          commentVideo: null,
          commentTime: null,
          productUid: null,
          productName: null,
          productImgUrl: null,
          dataStatus: null,
          isShow: "1",
          createUid: null,
          createName: null,
          createTime: null,
          updateUid: null,
          updateName: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.buyersshowUid)
        this.names = selection.map(item => item.productName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.tabUids = [];
        this.tabList = [];
        this.selUids = null;
        this.open = true;
        this.proTableOpen = false;
        this.title = "添加商品买家秀信息";

      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看商品买家秀信息";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const buyersshowUid = row.buyersshowUid || this.ids
        getBuyersshow(buyersshowUid).then(response => {
          this.form = response.data;
          this.selUids = this.form.productUid;
          let data = {
            productUid: this.form.productUid,
            productImg: this.form.productImgUrl,
            productName: this.form.productName
          };
          this.tabList.push(data);
          this.proTableOpen = true;
          this.open = true;
          this.title = "修改商品买家秀信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$refs.childs.selectImg();
            this.$refs.childss.selectImgs();
             if (this.form.buyImg == null || this.form.buyImg == "") {
               this.$message.error('请添加买家秀头像！')
               return;
             }
            if (this.form.imagesUrl == null || this.form.imagesUrl == "") {
              this.$message.error('请添加图片！')
              return;
            }
            if (this.form.productUid == null || this.form.productUid == "") {
              this.$message.error('请选择商品！')
            }
            if (this.form.buyersshowUid != null) {
              updateBuyersshow(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBuyersshow(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const buyersshowUids = row.buyersshowUid || this.ids;
        this.$confirm('是否确认删除商品名称为"' + this.names + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBuyersshow(buyersshowUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('product/buyersshow/export', {
          ...this.queryParams
        }, `product_buyersshow.xlsx`)
      }
    }
  };
</script>
<style lang="scss" scoped>
  .line_div {
    margin-bottom: 0px;
  }

  .show_commment_img {
    display: flex;
    flex-wrap: wrap;
  }

  .pro_evaluate_data {
    display: flex;
    align-items: center;
    font-size: 12px;

    .item_evaluate {
      display: flex;
      align-items: center;

      &:not(:first-child) {
        margin-left: 15px;
      }

      .start_icon {
        color: #EF9C00;
      }

      .el-icon-star-on {
        font-size: 14px;
      }

      .el-icon-star-off {
        font-size: 12px;
      }
    }
  }

  .show_pro_info {
    display: flex;

    .show_pro_title {
      color: #5DA9EF;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;

      .show_pro_title {
        width: 100%;
        display: inline-block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      &:hover {
        cursor: pointer;
      }
    }
  }

  .place-font {
    color: #999;
    font-size: 13px;
    position: relative;
  }
</style>
